/** @format */

import * as mysql from 'mysql';
import {config} from '../config';

let sql: any = null;
let connection: any = null;

function createConnection() {
  connection = mysql.createConnection(config.mysqlConfig);
  connection.connect();
  connection.on('error', () => {
    createConnection();
  });

  sql = (sqlStr: string): Promise<unknown> =>
    new Promise((resolve, reject) => {
      connection.query(sqlStr, (error: any, results: any, fields: any) => {
        if (error) {
          reject(error);
        } else {
          resolve({results, fields});
        }
      });
    });
}

createConnection();
export {connection, sql};
